package org.gwtEss.other;

import com.google.gwt.event.dom.client.HasMouseDownHandlers;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;

public abstract class Draggable implements HasDragEvents{
	protected HasMouseDownHandlers ft;
	private MouseDownHandler mouseDownHandler; 

	protected boolean isPreventDefault(int x, int y){
		return false; 
	}
	MouseDownHandler getMouseDownHandler(){
		if (mouseDownHandler == null)
			mouseDownHandler = new MouseDownHandler(){
			int startX, startY; 
			public void onMouseDown(MouseDownEvent event) {
				startX = event.getClientX();
				startY = event.getClientY(); 
				onDragStart(event.getClientX(), event.getClientY());
				if (isPreventDefault(event.getClientX(), event.getClientY()))
					event.preventDefault();
				else 
					return;
				DragTrackNew dt = new DragTrackNew(){

					@Override
					public void onMouseMove(int mouseX, int mouseY) {
						onDrag(mouseX, mouseY, startX - mouseX, startY - mouseY);
					}

			
					@Override
					public void onMouseUp(int mouseX, int mouseY) {
						onDragEnd(mouseX, mouseY, startX - mouseX, startY - mouseY);
					}

					};
			
			}
			
		};
		
		return mouseDownHandler; 
	}
	public void setWidget(HasMouseDownHandlers mouseDownWidget) {
		if (mouseDownWidget == ft)
			return;
		this.ft = mouseDownWidget; 
		ft.addMouseDownHandler(getMouseDownHandler());		
	}
	public Draggable(HasMouseDownHandlers mouseDownWidget) {
		setWidget(mouseDownWidget);
	}
	public Draggable() {}
//	public Dragger(RoundBorder mainPanel) {
//		this();
//		setWidget(mainPanel);
//	}
}
